package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.ProductConfigurationRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.ProductConfiguration;
import com.ndp.fb.rdb.model.ProductConfigurationExt;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.redis.api.RedisDistributedLockService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.PreparedStatement;
import java.util.List;

/**
 * Created by infi on 2015/1/29.
 */
@Service
public class ProductConfigurationRdbServicesImpl extends DaoTemplate implements ProductConfigurationRdbService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private RedisDistributedLockService redisDistributedLockService;

    @Override
    public List<ProductConfiguration> findByFbConnectionObjectId(Long connectionObjectId) {
        String sql = "SELECT * FROM product_configuration  WHERE fb_connection_object_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{connectionObjectId}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
    }

    public List<ProductConfiguration> findByFbConnectionObjectIdInUse(Long connectionObjectId, String userId) {
        String sql = "SELECT * FROM product_configuration a JOIN ad_publish_condition b ON a.id = b.product_configuration_id  WHERE a.fb_connection_object_id = ? AND b.status = 1 AND b.user_id=?";
        return getJdbcTemplate().query(sql, new Object[]{connectionObjectId, userId}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
    }

    @Override
    public List<ProductConfiguration> findByFbConnectionObjectIdWithOut(Long connectionObjectId) {
        String sql = "SELECT * FROM product_configuration  WHERE fb_connection_object_id = ? ";
        return getJdbcTemplate().query(sql, new Object[]{connectionObjectId}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
    }

    @Override
    public ProductConfiguration findById(Long productConfigurationId) {
        String sql = "SELECT * FROM product_configuration WHERE id = ?";
        List<ProductConfiguration> list = getJdbcTemplate().query(sql, new Object[]{productConfigurationId}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * @param objectStoryUrl
     * @return
     * @discription
     * @author yanghao
     * 创建时间 2015年3月19日 下午3:02:49
     * @see com.ndp.fb.rdb.api.ProductConfigurationRdbService#findByObjectStoryUrl(java.lang.String)
     */

    @Override
    public ProductConfiguration findByObjectStoryUrl(String objectStoryUrl) {
        String sql = "select * from product_configuration where object_store_url = ?";
        List<ProductConfiguration> list = getJdbcTemplate().query(sql, new Object[]{objectStoryUrl}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<ProductConfiguration> findAll() {
        String sql = "select * from product_configuration ";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
    }

    @Override
    public List<ProductConfigurationExt> findAllProductConfigurationExt() {
        String sql = "SELECT p.*,c.`name` from product_configuration p , connection_object c where p.fb_connection_object_id = c.fb_connection_object_id";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(ProductConfigurationExt.class));
    }

    /**
     * @param objectStoryUrl
     * @return
     * @discription
     * @author yanghao
     * 创建时间 2015年3月26日 下午7:50:33
     * @see com.ndp.fb.rdb.api.ProductConfigurationRdbService#findByObjectStoryUrlLike(java.lang.String)
     */

    @Override
    public ProductConfiguration findByObjectStoryUrlLike(String objectStoryUrl) {
        String sql = "select * from product_configuration where object_store_url like '%" + objectStoryUrl + "%'";
        List<ProductConfiguration> list = getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }


    /**
     * @param id
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年5月8日 下午3:50:36
     * @see com.ndp.fb.rdb.api.ProductConfigurationRdbService#findFromCache(java.lang.Long)
     */

    @Override
    public ProductConfiguration findFromCache(Long id) {
        if (null == id || id.equals(0L)) {
            return null;
        }
        ProductConfiguration productConfiguration = (ProductConfiguration) redisClient.get("ProductConfiguration" + id);
        if (productConfiguration == null) {
            productConfiguration = this.findById(id);
            redisClient.set("ProductConfiguration" + id, productConfiguration);
        }
        return productConfiguration;
    }

    /**
     * @param connectionObjectId
     * @param platform
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年6月8日 下午1:30:09
     * @see com.ndp.fb.rdb.api.ProductConfigurationRdbService#findByFbConnectionObjectIdAndPlatForm(java.lang.Long, java.lang.String)
     */

    @Override
    public ProductConfiguration findByFbConnectionObjectIdAndPlatForm(
            Long connectionObjectId, String platform) {
        String sql = "SELECT * FROM product_configuration WHERE fb_connection_object_id = ? AND platform = ?";
        return findOne(sql, new Object[]{connectionObjectId, platform}, DefaultRowMapper.autoRowMapper(ProductConfiguration.class));
    }

    /**
     * @param productConfiguration
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年6月8日 下午1:58:37
     * @see com.ndp.fb.rdb.api.ProductConfigurationRdbService#update(com.ndp.fb.rdb.model.ProductConfiguration)
     */

    @Override
    public Integer update(final ProductConfiguration productConfiguration) {
        return getJdbcTemplate().update(con -> {
            String sql = "update product_configuration set object_store_url=? where fb_connection_object_id =? and platform =?";
            PreparedStatement ps = con.prepareStatement(sql);

            ps.setObject(1, productConfiguration.getObjectStoreUrl());
            ps.setObject(2, productConfiguration.getFbConnectionObjectId());
            ps.setObject(3, productConfiguration.getPlatform());
            return ps;
        });
    }



    @Override
    public Long saveProductConfiguration(ProductConfiguration productConfiguration) {
        return saveResultId(productConfiguration);
    }
}
